#include "client.h"

int main(int argc, char **argv) {
   int retval, sock, fd;
   char menu[2];
   socklen_t len;
   msg* list = (msg*) malloc(sizeof(msg));
   app* append = (app*) malloc(sizeof(app));
   struct sockaddr_in serveraddr;
   struct dirent *direntp;
   direntp = malloc(sizeof(struct dirent));

   if (argc != 3)
      err_quit("usage: ./client <hostname or IPaddress> <service or port>");

   sock = socket(PF_INET, SOCK_STREAM, 0);
   if (sock == -1)
      err_quit("socket() error");
   serveraddr.sin_family = AF_INET;
   serveraddr.sin_port = htons(argv[2]);
   serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
   retval = connect(sock, (struct sockaddr*) &serveraddr, sizeof(serveraddr));
   if (retval == -1)
      err_quit("connect() error");

   while (1) {
      fflush(stdout);
      fflush(stdin);
      printf("Select menu (1. list 2. download file 3. append 4. exit) : ");
      scanf("%s", menu);
      write(sock, menu, 2);
      char buf[BUFSIZE];
      if (!strcmp(menu, "2")) {
         char filename[256];
         char ssend[BUFSIZE];

         memset(filename, 0, sizeof(filename));
         printf("File name : ");
         fflush(stdin);
         scanf("%s", filename);
         fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
         if (fd < 0) {
            err_quit("File I/O error");
            close(sock);
            return 1;
         }
         strcpy(append->name, filename);
         memcpy(ssend, append, sizeof(app));
         retval = write(sock, ssend, sizeof(app));
//              retval = recvn(sock, filename, 256);
         if (retval == -1) {
            err_quit("recv() error");
            close(sock);
            return 1;
         }

         int totalbytes;
         retval = recvn(sock, (char *) &totalbytes, sizeof(totalbytes));
         if (retval == -1) {
            err_quit("recv() error");
            close(sock);
            return 1;
         }
         printf("%d\n", totalbytes);
         int numtotal = 0;
         int sum = 0;
         int nwrite = 0;
         while (1) {
            sum = numtotal;
            if (sum == totalbytes) {
               fprintf(stderr, "com\n");
               break;
            }
            char readbuf[BUFSIZE] = { 0 };
            retval = read(sock, readbuf, BUFSIZE);
            if (retval == -1) {
               err_quit("recv() error");
            } else {
               nwrite = write(fd, readbuf, retval);
               numtotal += nwrite;
            }
         }
         close(fd);
         if (numtotal == totalbytes)
            fprintf(stderr, "-> File trans complete\n");
         else
            fprintf(stderr, "-> File trans failed\n");

         fprintf(stderr, "File receive complete\n");

      } else if (!strcmp(menu, "1")) {
         int totalbytes;
         while (1) {
            retval = read(sock, buf, sizeof(msg));
            memcpy(list, buf, sizeof(msg));
            if (list->size == -1) {
               break;
            }
            printf("file name = %s file size = %d\n", list->name,
                  list->size);

         }
      }
      else if (!strcmp(menu, "3")) {
         write(sock, menu, 2);
         printf("exit program\n");
         break;
      } else {
         printf("wrong menu\n");
      }
   }
   return 0;
}
